package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.TodoService;
import com.itheima.vo.TodoVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/todo")
@Api(value = "后台管理模块",description = "待办事项相关接口")
public class TodoController {

    @Autowired
    private TodoService todoService;

    /**
     * 增加代办事项
     * @param todoVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody TodoVo todoVo){
        try {
            todoService.add(todoVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","添加失败");
        }
        return new Result("success","添加成功");
    }

    /**
     * 删除待办事项
     * @param todoVo
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody TodoVo todoVo){
        try {
            todoService.delete(todoVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","删除失败");
        }

        return new Result("success","删除成功");
    }

    /**
     * 编辑待办事项
     * @param todoVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody TodoVo todoVo){
        try {
            todoService.edit(todoVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","编辑失败");
        }

        return new Result("success","编辑成功");
    }

    /**
     * 获取待办事项列表
     * @param todoVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody TodoVo todoVo){
        PageResult pageResult = todoService.getList(todoVo.getKeyword(),todoVo.getCurrentPage(),todoVo.getPageSize());
        return new Result("success",pageResult);
    }
}
